<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\report\ocean;

use app\enum\OeCreativeEnum;
use app\enum\ReportRecordEnum;
use app\model\ClientAccountModel;
use app\model\ClientAccountOperationTimeModel;
use app\model\ReportRecordPushOeAccountDateModel;
use app\model\ReportRecordPushOeAccountModel;
use app\model\ReportRecordPushOeCampaignModel;
use app\model\ReportRecordPushOeUnitModel;
use app\service\report\ReportService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

final class OeProcessingDataService extends ReportService
{
    /**
     * 数据处理.
     */
    public static function processingData(array $params = [])
    {
        try {
            $result = [];
            $dataArr = [];
            $totalArr = [];
            // 是否启用汇总数据：0不启用 1启用
            $is_sum = $params['is_sum'];
            // 保留历史数据(只用于时报)：0不勾选 1勾选
            $is_retain = $params['is_retain'];
            // 报表要导出的字段
            $add_fields = json_decode($params['add_fields'], true);
            // 发送的时间点
            $sendTime = $params['sendTime'];
            // 发送的日期
            $sendDate = $params['sendDate'];
            $sendYesterdayDate = strtotime(strval($sendDate)) - 86400;
            $sendBeforeYesterdayDate = strtotime(strval($sendDate)) - (2 * 86400);
            // 发送的时间点数组
            $send_time = json_decode($params['send_time'], true);
            // 数据维度 1账户 2广告组 3广告计划
            $dimension = $params['dimension'];
            // 报表排序 1账户 2消耗
            $dataSort = $params['data_sort'];
            // 报表类型：1时报；2日报 (日报就是昨天的所有的数据，时报就是当天截止到时间点的数据)
            $report_date_type = $params['report_date_type'];
            // 折算比例
            $cash_cost_ratio = $params['cash_cost_ratio'];
            // 折后成本-折算比例
            $folding_convert_ratio = $params['folding_convert_ratio'];
            // 折后成本-选择的字段
            $folding_convert_field = $params['folding_convert_field'];
            // 选择的账户ID数组
            $advertiserIds = json_decode($params['adv_unit_plan_lists'], true);
            // 自定义数据数组
            $custom_fields_name = $params['custom_fields_name'] ? json_decode($params['custom_fields_name'], true) : [];
            // 获取所有广告主的信息
            $advInfos = [];
            $productInfos = [];
            $advInfoss = (new ClientAccountModel())->alias('a')
                ->leftJoin(['habo_account_port' => 'b'], 'a.advertiser_id=b.advertiser_id')
                ->leftJoin(['habo_client_promotion_product' => 'c'], 'c.id=b.product_id')
                ->whereIn('a.advertiser_id', $advertiserIds)
                ->field('a.advertiser_id,a.account_name,c.product_name')
                ->select()
                ->toArray();
            if (! empty($advInfoss)) {
                foreach ($advInfoss as $val) {
                    $advInfos[$val['advertiser_id']] = $val['account_name'];
                    $productInfos[$val['advertiser_id']] = $val['product_name'];
                }
            }
            $yesterdayEventNextDayStayArr = [];
            $yesterdayChargeArr = [];
            $yesterdayActivationArr = [];
            $ciDayEventNextDayStayArr = [];
            $ciDayChargeArr = [];
            $ciDayActivationArr = [];
            $advertiserKey = 0;
            foreach ($advertiserIds as $advertiserId) {
                $eventNextDayStay = 0;
                $eventNextDayStayCost = 0;
                $eventNextDayStayActive = 0;
                $eventNextDayStayRatio = 0;
                $yesterdayEventNextDayStay = 0;
                $yesterdayCharge = 0;
                $yesterdayActivation = 0;
                $yesterdayEventNextDayStayCost = 0;
                $yesterdayEventNextDayStayRatio = 0;
                // 昨日次留
                if (in_array('attribution_next_day_open_rate', $add_fields) || in_array('attribution_next_day_open_cost', $add_fields) || in_array('attribution_next_day_open_cnt', $add_fields)) {
                    if ($report_date_type == 1) {
                        $statDate = date('Ymd', $sendYesterdayDate);
                    } else {
                        $statDate = date('Ymd', $sendBeforeYesterdayDate);
                    }
                    // 获取报表数据
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::getAccountSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::getCampaignSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::getUnitSecondaryStation(strval($advertiserId), intval($statDate), intval($sendTime), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取快手媒体数据失败');
                    }
                    // 判断当前页码是否获取全部数据
                    if (! empty($list)) {
                        // 次日次留数
                        $eventNextDayStay = $list['attribution_next_day_open_cnt'];
                        $eventNextDayStayCost = $list['cost'];
                        $eventNextDayStayActive = $list['active'];
                        $eventNextDayStayCost = ! empty($eventNextDayStay) ? round($eventNextDayStayCost / $eventNextDayStay, 3) : 0;
                        $eventNextDayStayRatio = ! empty($eventNextDayStayActive) ? round($eventNextDayStay / $eventNextDayStayActive * 100, 2) . '%' : 0;
                        $ciDayEventNextDayStayArr[] = $eventNextDayStay;
                        $ciDayChargeArr[] = $eventNextDayStayCost;
                        $ciDayActivationArr[] = $eventNextDayStayActive;
                        unset($list);
                    }
                }
                // 拼接参数
                if ($report_date_type == 1) {
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::importAccountReport(intval($sendDate), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::importCampaignReport(intval($sendDate), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::importUnitReport(intval($sendDate), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取巨量媒体数据失败');
                    }
                } else {
                    switch ($dimension) {
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_ACCOUNT:
                            $list = self::importAccountReportDate(intval(date('Ymd', $sendYesterdayDate)), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_CAMPAIGN:
                            $list = self::importCampaignReport(intval(date('Ymd', $sendYesterdayDate)), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        case $dimension == ReportRecordEnum::REPORT_DIMENSION_UNIT:
                            $list = self::importUnitReport(intval(date('Ymd', $sendYesterdayDate)), $sendTime, strval($advertiserId), intval($report_date_type));
                            break;
                        default:
                            return self::setError('获取巨量媒体数据失败');
                    }
                }
                $send_times = [];
                foreach ($send_time as $stk => $stv) {
                    if (strtotime($sendDate . ' ' . $stv . ':00') <= strtotime($sendDate . ' ' . $sendTime . ':00')) {
                        $send_times[$stk] = strtotime($sendDate . ' ' . $stv . ':00');
                    }
                }
                sort($send_times);
                // 格式化全部数据
                if (! empty($list)) {
                    // 日报需要计算0点到当前点的合计
                    $tmpArr = [];
                    if ($report_date_type == 1) {
                        // 保留历史数据
                        if ($is_retain == 1) {
                            foreach ($send_times as $stv) {
                                foreach ($list as $val) {
                                    // 小于当前时间点的数据拼入数组
                                    if ($stv >= strtotime($val['stat_datetime']) && $stv <= strtotime($sendDate . ' ' . $sendTime . ':00')) {
                                        if ($dimension == 1) {
                                            $tmpArr[$stv . '_' . $advertiserId][] = $val;
                                        } elseif ($dimension == 2) {
                                            $tmpArr[$stv . '_' . $val['campaign_id']][] = $val;
                                        } elseif ($dimension == 3) {
                                            $tmpArr[$stv . '_' . $val['ad_id']][] = $val;
                                        }
                                    }
                                }
                            }
                        } else {
                            $stv = strtotime($sendDate . ' ' . $sendTime . ':00');
                            foreach ($list as $val) {
                                // 小于当前时间点的数据拼入数组
                                if ($stv >= strtotime($val['stat_datetime'])) {
                                    if ($dimension == 1) {
                                        $tmpArr[$stv . '_' . $advertiserId][] = $val;
                                    } elseif ($dimension == 2) {
                                        $tmpArr[$stv . '_' . $val['campaign_id']][] = $val;
                                    } elseif ($dimension == 3) {
                                        $tmpArr[$stv . '_' . $val['ad_id']][] = $val;
                                    }
                                }
                            }
                        }
                        unset($list);
                        $list = [];
                        if (! empty($tmpArr)) {
                            $i = 0;
                            foreach ($tmpArr as $tak => $tav) {
                                $list[$i]['cost'] = array_sum(array_column($tav, 'cost'));
                                $list[$i]['show'] = array_sum(array_column($tav, 'show'));
                                $list[$i]['avg_show_cost'] = ! empty($list[$i]['show']) ? round($list[$i]['cost'] / $list[$i]['show'] * 1000, 2) : 0;
                                $list[$i]['click'] = array_sum(array_column($tav, 'click'));
                                $list[$i]['avg_click_cost'] = ! empty($list[$i]['click']) ? round($list[$i]['cost'] / $list[$i]['click'], 2) : 0;
                                $list[$i]['ctr'] = ! empty($list[$i]['show']) ? round($list[$i]['click'] / $list[$i]['show'] * 100, 2) . '%' : 0;
                                $list[$i]['convert'] = array_sum(array_column($tav, 'convert'));
                                $list[$i]['convert_cost'] = ! empty($list[$i]['convert']) ? round($list[$i]['cost'] / $list[$i]['convert'], 2) : 0;
                                $list[$i]['convert_rate'] = ! empty($list[$i]['click']) ? round($list[$i]['convert'] / $list[$i]['click'] * 100, 2) . '%' : 0;
                                $list[$i]['deep_convert'] = array_sum(array_column($tav, 'deep_convert'));
                                $list[$i]['deep_convert_cost'] = ! empty($list[$i]['deep_convert']) ? round($list[$i]['cost'] / $list[$i]['deep_convert'], 2) : 0;
                                $list[$i]['deep_convert_rate'] = ! empty($list[$i]['convert']) ? round($list[$i]['deep_convert'] / $list[$i]['convert'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_convert'] = array_sum(array_column($tav, 'attribution_convert'));
                                $list[$i]['attribution_convert_cost'] = ! empty($list[$i]['attribution_convert']) ? round($list[$i]['cost'] / $list[$i]['attribution_convert'], 2) : 0;
                                $list[$i]['attribution_deep_convert'] = array_sum(array_column($tav, 'attribution_deep_convert'));
                                $list[$i]['attribution_deep_convert_cost'] = ! empty($list[$i]['attribution_deep_convert']) ? round($list[$i]['cost'] / $list[$i]['attribution_deep_convert'], 2) : 0;
                                $list[$i]['download_start'] = array_sum(array_column($tav, 'download_start'));
                                $list[$i]['download_start_cost'] = ! empty($list[$i]['download_start']) ? round($list[$i]['cost'] / $list[$i]['download_start'], 2) : 0;
                                $list[$i]['download_start_rate'] = ! empty($list[$i]['click']) ? round($list[$i]['download_start'] / $list[$i]['click'] * 100, 2) . '%' : 0;
                                $list[$i]['download_finish'] = array_sum(array_column($tav, 'download_finish'));
                                $list[$i]['download_finish_cost'] = ! empty($list[$i]['download_finish']) ? round($list[$i]['cost'] / $list[$i]['download_finish'], 2) : 0;
                                $list[$i]['download_finish_rate'] = ! empty($list[$i]['download_start']) ? round($list[$i]['download_finish'] / $list[$i]['download_start'] * 100, 2) . '%' : 0;
                                $list[$i]['install_finish'] = array_sum(array_column($tav, 'install_finish'));
                                $list[$i]['install_finish_cost'] = ! empty($list[$i]['install_finish']) ? round($list[$i]['cost'] / $list[$i]['install_finish'], 2) : 0;
                                $list[$i]['install_finish_rate'] = ! empty($list[$i]['download_finish']) ? round($list[$i]['install_finish'] / $list[$i]['download_finish'] * 100, 2) . '%' : 0;
                                $list[$i]['active'] = array_sum(array_column($tav, 'active'));
                                $list[$i]['active_cost'] = ! empty($list[$i]['active']) ? round($list[$i]['cost'] / $list[$i]['active'], 2) : 0;
                                $list[$i]['active_rate'] = ! empty($list[$i]['click']) ? round($list[$i]['active'] / $list[$i]['click'] * 100, 2) . '%' : 0;
                                $list[$i]['register'] = array_sum(array_column($tav, 'register'));
                                $list[$i]['active_register_cost'] = ! empty($list[$i]['register']) ? round($list[$i]['cost'] / $list[$i]['register'], 2) : 0;
                                $list[$i]['active_register_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['register'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['game_addiction'] = array_sum(array_column($tav, 'game_addiction'));
                                $list[$i]['game_addiction_cost'] = ! empty($list[$i]['game_addiction']) ? round($list[$i]['cost'] / $list[$i]['game_addiction'], 2) : 0;
                                $list[$i]['game_addiction_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['game_addiction'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['pay_count'] = array_sum(array_column($tav, 'pay_count'));
                                $list[$i]['active_pay_cost'] = ! empty($list[$i]['pay_count']) ? round($list[$i]['cost'] / $list[$i]['pay_count'], 2) : 0;
                                $list[$i]['active_pay_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['pay_count'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['game_pay_count'] = array_sum(array_column($tav, 'game_pay_count'));
                                $list[$i]['game_pay_cost'] = ! empty($list[$i]['game_pay_count']) ? round($list[$i]['cost'] / $list[$i]['game_pay_count'], 2) : 0;
                                $list[$i]['game_pay_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['game_pay_count'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_game_pay_7d_count'] = array_sum(array_column($tav, 'attribution_game_pay_7d_count'));
                                $list[$i]['attribution_game_pay_7d_cost'] = ! empty($list[$i]['attribution_game_pay_7d_count']) ? round($list[$i]['cost'] / $list[$i]['attribution_game_pay_7d_count'], 2) : 0;
                                $list[$i]['attribution_active_pay_7d_per_count'] = array_sum(array_column($tav, 'attribution_active_pay_7d_per_count'));
                                // $list[$i]['attribution_next_day_open_cnt'] = array_sum(array_column($tav, 'attribution_next_day_open_cnt'));
                                // $list[$i]['attribution_next_day_open_cost'] = ! empty($list[$i]['attribution_next_day_open_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_next_day_open_cnt'], 2) : 0;
                                // $list[$i]['attribution_next_day_open_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_next_day_open_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;

                                $list[$i]['attribution_next_day_open_cnt'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                                $list[$i]['attribution_next_day_open_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                                $list[$i]['attribution_next_day_open_rate'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;

                                $list[$i]['attribution_retention_2d_cnt'] = array_sum(array_column($tav, 'attribution_retention_2d_cnt'));
                                $list[$i]['attribution_retention_2d_cost'] = ! empty($list[$i]['attribution_retention_2d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_2d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_2d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_2d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_retention_3d_cnt'] = array_sum(array_column($tav, 'attribution_retention_3d_cnt'));
                                $list[$i]['attribution_retention_3d_cost'] = ! empty($list[$i]['attribution_retention_3d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_3d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_3d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_3d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_retention_4d_cnt'] = array_sum(array_column($tav, 'attribution_retention_4d_cnt'));
                                $list[$i]['attribution_retention_4d_cost'] = ! empty($list[$i]['attribution_retention_4d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_4d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_4d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_4d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_retention_5d_cnt'] = array_sum(array_column($tav, 'attribution_retention_5d_cnt'));
                                $list[$i]['attribution_retention_5d_cost'] = ! empty($list[$i]['attribution_retention_5d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_5d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_5d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_5d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_retention_6d_cnt'] = array_sum(array_column($tav, 'attribution_retention_6d_cnt'));
                                $list[$i]['attribution_retention_6d_cost'] = ! empty($list[$i]['attribution_retention_6d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_6d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_6d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_6d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['attribution_retention_7d_cnt'] = array_sum(array_column($tav, 'attribution_retention_7d_cnt'));
                                $list[$i]['attribution_retention_7d_cost'] = ! empty($list[$i]['attribution_retention_7d_cnt']) ? round($list[$i]['cost'] / $list[$i]['attribution_retention_7d_cnt'], 2) : 0;
                                $list[$i]['attribution_retention_7d_rate'] = ! empty($list[$i]['active']) ? round($list[$i]['attribution_retention_7d_cnt'] / $list[$i]['active'] * 100, 2) . '%' : 0;
                                $list[$i]['loan_completion'] = array_sum(array_column($tav, 'loan_completion'));
                                $list[$i]['loan_completion_cost'] = ! empty($list[$i]['loan_completion']) ? round($list[$i]['cost'] / $list[$i]['loan_completion'], 2) : 0;
                                $list[$i]['loan_completion_rate'] = ! empty($list[$i]['register']) ? round($list[$i]['loan_completion'] / $list[$i]['register'] * 100, 2) . '%' : 0;
                                $list[$i]['pre_loan_credit'] = array_sum(array_column($tav, 'pre_loan_credit'));
                                $list[$i]['pre_loan_credit_cost'] = ! empty($list[$i]['pre_loan_credit']) ? round($list[$i]['cost'] / $list[$i]['pre_loan_credit'], 2) : 0;
                                $list[$i]['loan_credit'] = array_sum(array_column($tav, 'loan_credit'));
                                $list[$i]['loan_credit_cost'] = ! empty($list[$i]['loan_credit']) ? round($list[$i]['cost'] / $list[$i]['loan_credit'], 2) : 0;
                                $list[$i]['loan_credit_rate'] = ! empty($list[$i]['loan_completion']) ? round($list[$i]['loan_credit'] / $list[$i]['loan_completion'] * 100, 2) . '%' : 0;
                                $list[$i]['in_app_uv'] = array_sum(array_column($tav, 'in_app_uv'));
                                $list[$i]['in_app_detail_uv'] = array_sum(array_column($tav, 'in_app_detail_uv'));
                                $list[$i]['in_app_cart'] = array_sum(array_column($tav, 'in_app_cart'));
                                $list[$i]['in_app_pay'] = array_sum(array_column($tav, 'in_app_pay'));
                                $list[$i]['in_app_order'] = array_sum(array_column($tav, 'in_app_order'));
                                $list[$i]['phone'] = array_sum(array_column($tav, 'phone'));
                                $list[$i]['form'] = array_sum(array_column($tav, 'form'));
                                $list[$i]['total_play'] = array_sum(array_column($tav, 'total_play'));
                                $list[$i]['valid_play'] = array_sum(array_column($tav, 'valid_play'));
                                $list[$i]['valid_play_cost'] = ! empty($list[$i]['valid_play']) ? round($list[$i]['cost'] / $list[$i]['valid_play'], 2) : 0;
                                $list[$i]['valid_play_rate'] = ! empty($list[$i]['show']) ? round($list[$i]['valid_play'] / $list[$i]['show'] * 100, 2) . '%' : 0;
                                $list[$i]['share'] = array_sum(array_column($tav, 'share'));
                                $list[$i]['comment'] = array_sum(array_column($tav, 'comment'));
                                $list[$i]['like'] = array_sum(array_column($tav, 'like'));
                                $list[$i]['follow'] = array_sum(array_column($tav, 'follow'));
                                $list[$i]['message_action'] = array_sum(array_column($tav, 'message_action'));
                                $list[$i]['stat_datetime'] = date('Y-m-d H:00:00', intval(explode('_', $tak)[0]));

                                $list[$i]['attribution_game_in_app_ltv_1day'] = array_sum(array_column($tav, 'attribution_game_in_app_ltv_1day')); // 当日付费金额
                                $list[$i]['attribution_game_in_app_roi_1day'] = ! empty($list[$i]['cost']) ? round($list[$i]['attribution_game_in_app_ltv_1day'] / $list[$i]['cost'] * 100, 2) . '%' : 0; // 当日付费ROI
                                if ($dimension == 2) {
                                    $list[$i]['campaign_id'] = explode('_', $tak)[1];
                                    $list[$i]['campaign_name'] = $tav[0]['campaign_name'];
                                } elseif ($dimension == 3) {
                                    $list[$i]['ad_id'] = explode('_', $tak)[1];
                                    $list[$i]['ad_name'] = $tav[0]['ad_name'];
                                    $list[$i]['campaign_id'] = $tav[0]['campaign_id'];
                                    $list[$i]['campaign_name'] = $tav[0]['campaign_name'];
                                }
                                ++$i;
                            }
                        }
                    }
                }
                // 拼接基础数据
                if (! empty($list)) {
                    // 优化师
                    $operation = (new ClientAccountOperationTimeModel())->alias('a')->leftJoin(['habo_staff' => 'b'], 'a.staff_id=b.id')->where('a.advertiser_id', $advertiserId)->whereNull('a.end_date')->value('staff_name');
                    foreach ($list as $key => $item) {
                        $list[$key]['attribution_game_in_app_ltv_1day'] = ! empty($list[$key]['attribution_game_in_app_ltv_1day']) ? $list[$key]['attribution_game_in_app_ltv_1day'] : 0; // 当日付费金额
                        $list[$key]['attribution_game_in_app_roi_1day'] = ! empty($list[$key]['attribution_game_in_app_roi_1day']) ? $list[$key]['attribution_game_in_app_roi_1day'] : 0; // 当日付费ROI
                        // 优化师
                        $list[$key]['operation_name'] = $operation;
                        // 广告主信息
                        $list[$key]['advertiser_id'] = $advertiserId;
                        $list[$key]['account_name'] = ! empty($advInfos[$advertiserId]) ? $advInfos[$advertiserId] : '';
                        // 日期、时间

                        // 日期、时间
                        $list[$key]['date'] = $report_date_type == 1 ? date('Y-m-d', strtotime(strval($sendDate))) : date('Y-m-d', $sendYesterdayDate);

                        $list[$key]['time'] = $sendTime . ':00';
                        if ($is_retain == 1) {
                            $list[$key]['time'] = date('H:00', strtotime($item['stat_datetime']));
                        }
                        $list[$key]['stat_hour'] = intval(date('H', strtotime($item['stat_datetime'])));
                        // 产品
                        $list[$key]['product_name'] = ! empty($productInfos[$advertiserId]) ? $productInfos[$advertiserId] : '';
                        // 代理商
                        $list[$key]['agent'] = '上海艾秒广告有限公司';
                        // 媒体
                        $list[$key]['media'] = '头条-巨量引擎';
                        $list[$key]['data_sort'] = $advertiserKey;
                        if ($dimension == 1) {
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'cost');
                                $list[$key]['yesterday_cost'] = ! empty($list[$key]['yesterday_cost']) ? $list[$key]['yesterday_cost'] : 0;
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['cost']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_rate'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('active_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['active_pay_cost_yesterday'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'active_pay_cost');
                                $list[$key]['active_pay_cost_yesterday'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? $list[$key]['active_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['active_pay_ringgit'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? round($list[$key]['active_pay_cost'] / $list[$key]['active_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                        } elseif ($dimension == 2) {
                            $data = (new ReportRecordPushOeCampaignModel())->where('advertiser_id', $advertiserId)->where('campaign_id', $val['campaign_id'])->find()->toArray();
                            $list[$key]['landing_type'] = 0;
                            $list[$key]['budget'] = 0;
                            $list[$key]['create_time'] = 0;
                            if (! empty($data)) {
                                $dataContent = json_decode($data['data_content'], true);
                                // 日预算、创建时间
                                $info = [
                                    'budget' => $dataContent['budget'],
                                    'campaign_create_time' => $dataContent['campaign_create_time'],
                                    'landing_type' => $dataContent['campaign_create_time'],
                                ];
                                $list[$key]['landing_type'] = ! empty($info['landing_type']) ? $info['landing_type'] : 0;
                                $list[$key]['budget'] = ! empty($info['budget']) ? $info['budget'] / 1000 : 0;
                                $list[$key]['create_time'] = ! empty($info['campaign_create_time']) ? $info['campaign_create_time'] : 0;
                            }
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getCampaignCost($advertiserId, $val['campaign_id'], intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'cost');
                                $list[$key]['yesterday_cost'] = ! empty($list[$key]['yesterday_cost']) ? $list[$key]['yesterday_cost'] : 0;
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['cost']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_rate'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('active_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['active_pay_cost_yesterday'] = self::getCampaignCost($advertiserId, $val['campaign_id'], intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'active_pay_cost');
                                $list[$key]['active_pay_cost_yesterday'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? $list[$key]['active_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['active_pay_ringgit'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? round($list[$key]['active_pay_cost'] / $list[$key]['active_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                        } elseif ($dimension == 3) {
                            // 出价、ROI系数、日预算、创建时间、优化目标、深度转化目标
                            $info = (new ReportRecordPushOeUnitModel())->where('advertiser_id', $advertiserId)
                                ->where('ad_id', $item['ad_id'])
                                ->column('data_content');
                            $list[$key]['roi_goal'] = 0;
                            $list[$key]['landing_type'] = 0;
                            $list[$key]['deep_external_action'] = 0;
                            $list[$key]['status'] = ! empty($info['status']) ? $info['status'] : 0;
                            $list[$key]['budget'] = ! empty($info['budget']) ? $info['budget'] / 1000 : 0;
                            $list[$key]['convert_id'] = ! empty($info['convert_id']) ? $info['convert_id'] : 0;
                            $list[$key]['deep_cpabid'] = ! empty($info['deep_cpabid']) ? $info['deep_cpabid'] : 0;
                            $list[$key]['create_time'] = ! empty($info['ad_create_time']) ? $info['ad_create_time'] : 0;
                            $list[$key]['download_type'] = ! empty($info['download_type']) ? $info['download_type'] : 0;
                            $list[$key]['smart_bid_type'] = ! empty($info['smart_bid_type']) ? $info['smart_bid_type'] : 0;
                            $list[$key]['campaign_id'] = ! empty($item['campaign_id']) ? $item['campaign_id'] : '';
                            $list[$key]['campaign_name'] = ! empty($item['campaign_name']) ? $item['campaign_name'] : '';
                            if (! empty($info['inventory_type'])) {
                                $inventory_type = json_decode($info['inventory_type'], true);
                                foreach ($inventory_type as $kk => $vv) {
                                    $inventory_type[$kk] = OeCreativeEnum::INVENTORY_INFO[trim($vv, '"')];
                                }
                                $inventory_type = json_encode($inventory_type, JSON_UNESCAPED_UNICODE);
                            }
                            $list[$key]['inventory_type'] = ! empty($info['inventory_type']) ? $inventory_type : 0;
                            $list[$key]['delivery_range'] = ! empty($info['delivery_range']) ? $info['delivery_range'] : 0;
                            $list[$key]['bid'] = ! empty($info['bid']) ? $info['bid'] : 0;
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$key]['yesterday_cost'] = self::getUnitCost($advertiserId, $val['unit_id'], intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'cost');
                                $list[$key]['yesterday_cost'] = ! empty($list[$key]['yesterday_cost']) ? $list[$key]['yesterday_cost'] : 0;
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($item['cost']), strval($list[$key]['yesterday_cost']), 3);
                                $list[$key]['yesterday_cost_rate'] = ! empty($list[$key]['yesterday_cost']) ? round($x / $list[$key]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('active_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$key]['active_pay_cost_yesterday'] = self::getUnitCost($advertiserId, $val['unit_id'], intval(date('Ymd', $sendYesterdayDate)), intval($list[$key]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'active_pay_cost');
                                $list[$key]['active_pay_cost_yesterday'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? $list[$key]['active_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$key]['active_pay_ringgit'] = ! empty($list[$key]['active_pay_cost_yesterday']) ? round($list[$key]['active_pay_cost'] / $list[$key]['active_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                        }
                        // 折后消耗
                        if (in_array('cash_cost', $add_fields)) {
                            $list[$key]['cash_cost'] = ! empty($cash_cost_ratio) ? round($item['cost'] / $cash_cost_ratio, 2) : 0;
                        }
                        // 折后成本
                        if (in_array('folding_convert', $add_fields)) {
                            $list[$key]['folding_convert'] = ! empty($folding_convert_ratio) ? round($item[$folding_convert_field] / $folding_convert_ratio, 2) : 0;
                        }
                        // 自定义数据
                        if (! empty($custom_fields_name)) {
                            foreach ($custom_fields_name as $cfnv) {
                                $molecule = ! empty($list[$key][$cfnv['molecule']]) ? str_replace('%', '', $list[$key][$cfnv['molecule']]) : 0;
                                $denominator = ! empty($list[$key][$cfnv['denominator']]) ? str_replace('%', '', $list[$key][$cfnv['denominator']]) : 0;
                                $list[$key][$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                            }
                        }
                    }
                }

                $emptySendTimes = array_diff($send_time, array_column($list, 'time'));

                if ($emptySendTimes) {
                    foreach ($emptySendTimes as $sv) {
                        if (strtotime($sendDate . ' ' . $sv . ':00') > strtotime($sendDate . ' ' . $sendTime . ':00') || ! ($report_date_type == 1 && $is_retain == 1)) {
                            continue;
                        }
                        // 数据补零
                        if ($dimension == 1) {
                            $i = count($list);
                            $list[$i]['cost'] = ! empty($list[$i]['cost']) ? $list[$i]['cost'] : 0;
                            $list[$i]['show'] = ! empty($list[$i]['show']) ? $list[$i]['show'] : 0;
                            $list[$i]['avg_show_cost'] = ! empty($list[$i]['avg_show_cost']) ? $list[$i]['avg_show_cost'] : 0;
                            $list[$i]['click'] = ! empty($list[$i]['click']) ? $list[$i]['click'] : 0;
                            $list[$i]['avg_click_cost'] = ! empty($list[$i]['avg_click_cost']) ? $list[$i]['avg_click_cost'] : 0;
                            $list[$i]['ctr'] = ! empty($list[$i]['ctr']) ? $list[$i]['ctr'] : 0;
                            $list[$i]['convert'] = ! empty($list[$i]['convert']) ? $list[$i]['convert'] : 0;
                            $list[$i]['convert_cost'] = ! empty($list[$i]['convert_cost']) ? $list[$i]['convert_cost'] : 0;
                            $list[$i]['convert_rate'] = ! empty($list[$i]['convert_rate']) ? $list[$i]['convert_rate'] : 0;
                            $list[$i]['deep_convert'] = ! empty($list[$i]['deep_convert']) ? $list[$i]['deep_convert'] : 0;
                            $list[$i]['deep_convert_cost'] = ! empty($list[$i]['deep_convert_cost']) ? $list[$i]['deep_convert_cost'] : 0;
                            $list[$i]['deep_convert_rate'] = ! empty($list[$i]['deep_convert_rate']) ? $list[$i]['deep_convert_rate'] : 0;
                            $list[$i]['attribution_convert'] = ! empty($list[$i]['attribution_convert']) ? $list[$i]['attribution_convert'] : 0;
                            $list[$i]['attribution_convert_cost'] = ! empty($list[$i]['attribution_convert_cost']) ? $list[$i]['attribution_convert_cost'] : 0;
                            $list[$i]['attribution_deep_convert'] = ! empty($list[$i]['attribution_deep_convert']) ? $list[$i]['attribution_deep_convert'] : 0;
                            $list[$i]['attribution_deep_convert_cost'] = ! empty($list[$i]['attribution_deep_convert_cost']) ? $list[$i]['attribution_deep_convert_cost'] : 0;
                            $list[$i]['download_start'] = ! empty($list[$i]['download_start']) ? $list[$i]['download_start'] : 0;
                            $list[$i]['download_start_cost'] = ! empty($list[$i]['download_start_cost']) ? $list[$i]['download_start_cost'] : 0;
                            $list[$i]['download_start_rate'] = ! empty($list[$i]['download_start_rate']) ? $list[$i]['download_start_rate'] : 0;
                            $list[$i]['download_finish'] = ! empty($list[$i]['download_finish']) ? $list[$i]['download_finish'] : 0;
                            $list[$i]['download_finish_cost'] = ! empty($list[$i]['download_finish_cost']) ? $list[$i]['download_finish_cost'] : 0;
                            $list[$i]['download_finish_rate'] = ! empty($list[$i]['download_finish_rate']) ? $list[$i]['download_finish_rate'] : 0;
                            $list[$i]['install_finish'] = ! empty($list[$i]['install_finish']) ? $list[$i]['install_finish'] : 0;
                            $list[$i]['install_finish_cost'] = ! empty($list[$i]['install_finish_cost']) ? $list[$i]['install_finish_cost'] : 0;
                            $list[$i]['install_finish_rate'] = ! empty($list[$i]['install_finish_rate']) ? $list[$i]['install_finish_rate'] : 0;
                            $list[$i]['active'] = ! empty($list[$i]['active']) ? $list[$i]['active'] : 0;
                            $list[$i]['active_cost'] = ! empty($list[$i]['active_cost']) ? $list[$i]['active_cost'] : 0;
                            $list[$i]['active_rate'] = ! empty($list[$i]['active_rate']) ? $list[$i]['active_rate'] : 0;
                            $list[$i]['register'] = ! empty($list[$i]['register']) ? $list[$i]['register'] : 0;
                            $list[$i]['active_register_cost'] = ! empty($list[$i]['active_register_cost']) ? $list[$i]['active_register_cost'] : 0;
                            $list[$i]['active_register_rate'] = ! empty($list[$i]['active_register_rate']) ? $list[$i]['active_register_rate'] : 0;
                            // $list[$i]['attribution_next_day_open_cnt'] = ! empty($list[$i]['attribution_next_day_open_cnt']) ? $list[$i]['attribution_next_day_open_cnt'] : 0;
                            // $list[$i]['attribution_next_day_open_cost'] = ! empty($list[$i]['attribution_next_day_open_cost']) ? $list[$i]['attribution_next_day_open_cost'] : 0;
                            // $list[$i]['attribution_next_day_open_rate'] = ! empty($list[$i]['attribution_next_day_open_rate']) ? $list[$i]['attribution_next_day_open_rate'] : 0;

                            $list[$i]['attribution_next_day_open_cnt'] = ! empty($eventNextDayStay) ? $eventNextDayStay : 0;
                            $list[$i]['attribution_next_day_open_cost'] = ! empty($eventNextDayStayCost) ? $eventNextDayStayCost : 0;
                            $list[$i]['attribution_next_day_open_rate'] = ! empty($eventNextDayStayRatio) ? $eventNextDayStayRatio : 0;

                            $list[$i]['attribution_retention_2d_cnt'] = ! empty($list[$i]['attribution_retention_2d_cnt']) ? $list[$i]['attribution_retention_2d_cnt'] : 0;
                            $list[$i]['attribution_retention_2d_cost'] = ! empty($list[$i]['attribution_retention_2d_cost']) ? $list[$i]['attribution_retention_2d_cost'] : 0;
                            $list[$i]['attribution_retention_2d_rate'] = ! empty($list[$i]['attribution_retention_2d_rate']) ? $list[$i]['attribution_retention_2d_rate'] : 0;
                            $list[$i]['attribution_retention_3d_cnt'] = ! empty($list[$i]['attribution_retention_3d_cnt']) ? $list[$i]['attribution_retention_3d_cnt'] : 0;
                            $list[$i]['attribution_retention_3d_cost'] = ! empty($list[$i]['attribution_retention_3d_cost']) ? $list[$i]['attribution_retention_3d_cost'] : 0;
                            $list[$i]['attribution_retention_3d_rate'] = ! empty($list[$i]['attribution_retention_3d_rate']) ? $list[$i]['attribution_retention_3d_rate'] : 0;
                            $list[$i]['attribution_retention_4d_cnt'] = ! empty($list[$i]['attribution_retention_4d_cnt']) ? $list[$i]['attribution_retention_4d_cnt'] : 0;
                            $list[$i]['attribution_retention_4d_cost'] = ! empty($list[$i]['attribution_retention_4d_cost']) ? $list[$i]['attribution_retention_4d_cost'] : 0;
                            $list[$i]['attribution_retention_4d_rate'] = ! empty($list[$i]['attribution_retention_4d_rate']) ? $list[$i]['attribution_retention_4d_rate'] : 0;
                            $list[$i]['attribution_retention_5d_cnt'] = ! empty($list[$i]['attribution_retention_5d_cnt']) ? $list[$i]['attribution_retention_5d_cnt'] : 0;
                            $list[$i]['attribution_retention_5d_cost'] = ! empty($list[$i]['attribution_retention_5d_cost']) ? $list[$i]['attribution_retention_5d_cost'] : 0;
                            $list[$i]['attribution_retention_5d_rate'] = ! empty($list[$i]['attribution_retention_5d_rate']) ? $list[$i]['attribution_retention_5d_rate'] : 0;
                            $list[$i]['attribution_retention_6d_cnt'] = ! empty($list[$i]['attribution_retention_6d_cnt']) ? $list[$i]['attribution_retention_6d_cnt'] : 0;
                            $list[$i]['attribution_retention_6d_cost'] = ! empty($list[$i]['attribution_retention_6d_cost']) ? $list[$i]['attribution_retention_6d_cost'] : 0;
                            $list[$i]['attribution_retention_6d_rate'] = ! empty($list[$i]['attribution_retention_6d_rate']) ? $list[$i]['attribution_retention_6d_rate'] : 0;
                            $list[$i]['attribution_retention_7d_cnt'] = ! empty($list[$i]['attribution_retention_7d_cnt']) ? $list[$i]['attribution_retention_7d_cnt'] : 0;
                            $list[$i]['attribution_retention_7d_cost'] = ! empty($list[$i]['attribution_retention_7d_cost']) ? $list[$i]['attribution_retention_7d_cost'] : 0;
                            $list[$i]['attribution_retention_7d_rate'] = ! empty($list[$i]['attribution_retention_7d_rate']) ? $list[$i]['attribution_retention_7d_rate'] : 0;
                            $list[$i]['game_addiction'] = ! empty($list[$i]['game_addiction']) ? $list[$i]['game_addiction'] : 0;
                            $list[$i]['game_addiction_cost'] = ! empty($list[$i]['game_addiction_cost']) ? $list[$i]['game_addiction_cost'] : 0;
                            $list[$i]['game_addiction_rate'] = ! empty($list[$i]['game_addiction_rate']) ? $list[$i]['game_addiction_rate'] : 0;
                            $list[$i]['pay_count'] = ! empty($list[$i]['pay_count']) ? $list[$i]['pay_count'] : 0;
                            $list[$i]['active_pay_cost'] = ! empty($list[$i]['active_pay_cost']) ? $list[$i]['active_pay_cost'] : 0;
                            $list[$i]['active_pay_rate'] = ! empty($list[$i]['active_pay_rate']) ? $list[$i]['active_pay_rate'] : 0;
                            $list[$i]['game_pay_cost'] = ! empty($list[$i]['game_pay_cost']) ? $list[$i]['game_pay_cost'] : 0;
                            $list[$i]['game_pay_count'] = ! empty($list[$i]['game_pay_count']) ? $list[$i]['game_pay_count'] : 0;
                            $list[$i]['attribution_game_pay_7d_count'] = ! empty($list[$i]['attribution_game_pay_7d_count']) ? $list[$i]['attribution_game_pay_7d_count'] : 0;
                            $list[$i]['attribution_game_pay_7d_cost'] = ! empty($list[$i]['attribution_game_pay_7d_cost']) ? $list[$i]['attribution_game_pay_7d_cost'] : 0;
                            $list[$i]['attribution_active_pay_7d_per_count'] = ! empty($list[$i]['attribution_active_pay_7d_per_count']) ? $list[$i]['attribution_active_pay_7d_per_count'] : 0;
                            $list[$i]['loan_completion'] = ! empty($list[$i]['loan_completion']) ? $list[$i]['loan_completion'] : 0;
                            $list[$i]['loan_completion_cost'] = ! empty($list[$i]['loan_completion_cost']) ? $list[$i]['loan_completion_cost'] : 0;
                            $list[$i]['loan_completion_rate'] = ! empty($list[$i]['loan_completion_rate']) ? $list[$i]['loan_completion_rate'] : 0;
                            $list[$i]['pre_loan_credit'] = ! empty($list[$i]['pre_loan_credit']) ? $list[$i]['pre_loan_credit'] : 0;
                            $list[$i]['pre_loan_credit_cost'] = ! empty($list[$i]['pre_loan_credit_cost']) ? $list[$i]['pre_loan_credit_cost'] : 0;
                            $list[$i]['loan_credit'] = ! empty($list[$i]['loan_credit']) ? $list[$i]['loan_credit'] : 0;
                            $list[$i]['loan_credit_cost'] = ! empty($list[$i]['loan_credit_cost']) ? $list[$i]['loan_credit_cost'] : 0;
                            $list[$i]['loan_credit_rate'] = ! empty($list[$i]['loan_credit_rate']) ? $list[$i]['loan_credit_rate'] : 0;
                            $list[$i]['in_app_uv'] = ! empty($list[$i]['in_app_uv']) ? $list[$i]['in_app_uv'] : 0;
                            $list[$i]['in_app_detail_uv'] = ! empty($list[$i]['in_app_detail_uv']) ? $list[$i]['in_app_detail_uv'] : 0;
                            $list[$i]['in_app_cart'] = ! empty($list[$i]['in_app_cart']) ? $list[$i]['in_app_cart'] : 0;
                            $list[$i]['in_app_pay'] = ! empty($list[$i]['in_app_pay']) ? $list[$i]['in_app_pay'] : 0;
                            $list[$i]['in_app_order'] = ! empty($list[$i]['in_app_order']) ? $list[$i]['in_app_order'] : 0;
                            $list[$i]['phone'] = ! empty($list[$i]['phone']) ? $list[$i]['phone'] : 0;
                            $list[$i]['form'] = ! empty($list[$i]['form']) ? $list[$i]['form'] : 0;
                            $list[$i]['total_play'] = ! empty($list[$i]['total_play']) ? $list[$i]['total_play'] : 0;
                            $list[$i]['valid_play'] = ! empty($list[$i]['valid_play']) ? $list[$i]['valid_play'] : 0;
                            $list[$i]['valid_play_cost'] = ! empty($list[$i]['valid_play_cost']) ? $list[$i]['valid_play_cost'] : 0;
                            $list[$i]['valid_play_rate'] = ! empty($list[$i]['valid_play_rate']) ? $list[$i]['valid_play_rate'] : 0;
                            $list[$i]['share'] = ! empty($list[$i]['share']) ? $list[$i]['share'] : 0;
                            $list[$i]['comment'] = ! empty($list[$i]['comment']) ? $list[$i]['comment'] : 0;
                            $list[$i]['like'] = ! empty($list[$i]['like']) ? $list[$i]['like'] : 0;
                            $list[$i]['follow'] = ! empty($list[$i]['follow']) ? $list[$i]['follow'] : 0;
                            $list[$i]['message_actio'] = ! empty($list[$i]['message_actio']) ? $list[$i]['message_actio'] : 0;

                            $list[$i]['attribution_game_in_app_ltv_1day'] = ! empty($list[$i]['attribution_game_in_app_ltv_1day']) ? $list[$i]['attribution_game_in_app_ltv_1day'] : 0; // 当日付费金额
                            $list[$i]['attribution_game_in_app_roi_1day'] = ! empty($list[$i]['attribution_game_in_app_roi_1day']) ? $list[$i]['attribution_game_in_app_roi_1day'] : 0; // 当日付费ROI
                            // 获取基础数据、昨日消耗和环比
                            // 优化师
                            $list[$i]['operation_name'] = (new ClientAccountOperationTimeModel())->alias('a')->leftJoin(['habo_staff' => 'b'], 'a.staff_id=b.id')->where('a.advertiser_id', $advertiserId)->whereNull('a.end_date')->value('staff_name');
                            // 广告主信息
                            $list[$i]['advertiser_id'] = $advertiserId;
                            $list[$i]['account_name'] = ! empty($advInfos[$advertiserId]) ? $advInfos[$advertiserId] : '';
                            // 日期、时间
                            $list[$i]['date'] = $report_date_type == 1 ? date('Y-m-d', strtotime(strval($sendDate))) : date('Y-m-d', $sendYesterdayDate);
                            $list[$i]['time'] = $sv;
                            $list[$i]['stat_hour'] = intval(date('H', strtotime($sendDate . ' ' . $sv . ':00')));
                            // 产品
                            $list[$i]['product_name'] = ! empty($productInfos[$advertiserId]) ? $productInfos[$advertiserId] : '';
                            // 代理商
                            $list[$i]['agent'] = '上海艾秒广告有限公司';
                            // 媒体
                            $list[$i]['media'] = '快手-磁力引擎';
                            $list[$i]['data_sort'] = $advertiserKey;
                            // 昨日同时段消耗、昨日同时段消耗环比
                            if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                                // 昨日同时段消耗
                                $list[$i]['yesterday_cost'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$i]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'cost');
                                $list[$i]['yesterday_cost'] = ! empty($list[$i]['yesterday_cost']) ? $list[$i]['yesterday_cost'] : 0;
                                // 昨日同时段消耗环比
                                $x = bcsub(strval($list[$i]['cost']), strval($list[$i]['yesterday_cost']), 3);
                                $list[$i]['yesterday_cost_rate'] = ! empty($list[$i]['yesterday_cost']) ? round($x / $list[$i]['yesterday_cost'] * 100, 2) . '%' : 0;
                            }
                            // 付费成本环比
                            if (in_array('active_pay_ringgit', $add_fields)) {
                                // 昨日同时段新增付费人数成本
                                $list[$i]['active_pay_cost_yesterday'] = self::getAccountCost($advertiserId, intval(date('Ymd', $sendYesterdayDate)), intval($list[$i]['stat_hour']), ReportRecordEnum::REPORT_DATE_TYPE_TIME, 'active_pay_cost');
                                $list[$i]['active_pay_cost_yesterday'] = ! empty($list[$i]['active_pay_cost_yesterday']) ? $list[$i]['active_pay_cost_yesterday'] : 0;
                                // 付费成本环比
                                $list[$i]['active_pay_ringgit'] = ! empty($list[$i]['active_pay_cost_yesterday']) ? round($list[$i]['active_pay_cost'] / $list[$i]['active_pay_cost_yesterday'] - 1, 2) : 0;
                            }
                            // 折后消耗
                            if (in_array('cash_cost', $add_fields)) {
                                $list[$i]['cash_cost'] = ! empty($cash_cost_ratio) ? round($list[$i]['cost'] / $cash_cost_ratio, 2) : 0;
                            }
                            // 折后成本
                            if (in_array('folding_convert', $add_fields)) {
                                $list[$i]['folding_convert'] = ! empty($folding_convert_ratio) ? round($list[$i][$folding_convert_field] / $folding_convert_ratio, 2) : 0;
                            }
                            // 自定义数据
                            if (! empty($custom_fields_name)) {
                                foreach ($custom_fields_name as $cfnv) {
                                    $molecule = ! empty($list[$i][$cfnv['molecule']]) ? str_replace('%', '', $list[$i][$cfnv['molecule']]) : 0;
                                    $denominator = ! empty($list[$i][$cfnv['denominator']]) ? str_replace('%', '', $list[$i][$cfnv['denominator']]) : 0;
                                    $list[$i][$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                                }
                            }
                        }
                    }
                }
                ++$advertiserKey;
                $dataArr = array_merge($dataArr, $list);
            }
            array_multisort(array_column($dataArr, 'stat_hour'), SORT_ASC, array_column($dataArr, 'data_sort'), SORT_ASC, $dataArr);
            if ($dataSort == 2 && in_array('cost', $add_fields)) {
                array_multisort(array_column($dataArr, 'cost'), SORT_DESC, $dataArr);
            }
            // 拼接汇总数据
            if (! empty($dataArr)) {
                if ($is_sum == 1) {
                    $dataArrs = [];
                    foreach ($dataArr as $val) {
                        if ($val['time'] == $sendTime) {
                            $dataArrs[] = $val;
                        }
                    }
                    $totalArr['cost'] = ! empty($totalArr['cost']) ? $totalArr['cost'] : 0;
                    $totalArr['show'] = ! empty($totalArr['show']) ? $totalArr['show'] : 0;
                    $totalArr['avg_show_cost'] = ! empty($totalArr['avg_show_cost']) ? $totalArr['avg_show_cost'] : 0;
                    $totalArr['click'] = ! empty($totalArr['click']) ? $totalArr['click'] : 0;
                    $totalArr['avg_click_cost'] = ! empty($totalArr['avg_click_cost']) ? $totalArr['avg_click_cost'] : 0;
                    $totalArr['ctr'] = ! empty($totalArr['ctr']) ? $totalArr['ctr'] : 0;
                    $totalArr['cash_cost'] = ! empty($totalArr['cash_cost']) ? $totalArr['cash_cost'] : 0;
                    $totalArr['folding_convert'] = ! empty($totalArr['folding_convert']) ? $totalArr['folding_convert'] : 0;
                    $totalArr['convert'] = ! empty($totalArr['convert']) ? $totalArr['convert'] : 0;
                    $totalArr['convert_cost'] = ! empty($totalArr['convert_cost']) ? $totalArr['convert_cost'] : 0;
                    $totalArr['convert_rate'] = ! empty($totalArr['convert_rate']) ? $totalArr['convert_rate'] : 0;
                    $totalArr['deep_convert'] = ! empty($totalArr['deep_convert']) ? $totalArr['deep_convert'] : 0;
                    $totalArr['deep_convert_cost'] = ! empty($totalArr['deep_convert_cost']) ? $totalArr['deep_convert_cost'] : 0;
                    $totalArr['deep_convert_rate'] = ! empty($totalArr['deep_convert_rate']) ? $totalArr['deep_convert_rate'] : 0;
                    $totalArr['attribution_convert'] = ! empty($totalArr['attribution_convert']) ? $totalArr['attribution_convert'] : 0;
                    $totalArr['attribution_convert_cost'] = ! empty($totalArr['attribution_convert_cost']) ? $totalArr['attribution_convert_cost'] : 0;
                    $totalArr['attribution_deep_convert'] = ! empty($totalArr['attribution_deep_convert']) ? $totalArr['attribution_deep_convert'] : 0;
                    $totalArr['attribution_deep_convert_cost'] = ! empty($totalArr['attribution_deep_convert_cost']) ? $totalArr['attribution_deep_convert_cost'] : 0;
                    $totalArr['download_start'] = ! empty($totalArr['download_start']) ? $totalArr['download_start'] : 0;
                    $totalArr['download_start_cost'] = ! empty($totalArr['download_start_cost']) ? $totalArr['download_start_cost'] : 0;
                    $totalArr['download_start_rate'] = ! empty($totalArr['download_start_rate']) ? $totalArr['download_start_rate'] : 0;
                    $totalArr['download_finish'] = ! empty($totalArr['download_finish']) ? $totalArr['download_finish'] : 0;
                    $totalArr['download_finish_cost'] = ! empty($totalArr['download_finish_cost']) ? $totalArr['download_finish_cost'] : 0;
                    $totalArr['download_finish_rate'] = ! empty($totalArr['download_finish_rate']) ? $totalArr['download_finish_rate'] : 0;
                    $totalArr['install_finish'] = ! empty($totalArr['install_finish']) ? $totalArr['install_finish'] : 0;
                    $totalArr['install_finish_cost'] = ! empty($totalArr['install_finish_cost']) ? $totalArr['install_finish_cost'] : 0;
                    $totalArr['install_finish_rate'] = ! empty($totalArr['install_finish_rate']) ? $totalArr['install_finish_rate'] : 0;
                    $totalArr['active'] = ! empty($totalArr['active']) ? $totalArr['active'] : 0;
                    $totalArr['active_cost'] = ! empty($totalArr['active_cost']) ? $totalArr['active_cost'] : 0;
                    $totalArr['active_rate'] = ! empty($totalArr['active_rate']) ? $totalArr['active_rate'] : 0;
                    $totalArr['register'] = ! empty($totalArr['register']) ? $totalArr['register'] : 0;
                    $totalArr['active_register_cost'] = ! empty($totalArr['active_register_cost']) ? $totalArr['active_register_cost'] : 0;
                    $totalArr['active_register_rate'] = ! empty($totalArr['active_register_rate']) ? $totalArr['active_register_rate'] : 0;
                    $totalArr['attribution_next_day_open_cnt'] = ! empty($totalArr['attribution_next_day_open_cnt']) ? $totalArr['attribution_next_day_open_cnt'] : 0;
                    $totalArr['attribution_next_day_open_cost'] = ! empty($totalArr['attribution_next_day_open_cost']) ? $totalArr['attribution_next_day_open_cost'] : 0;
                    $totalArr['attribution_next_day_open_rate'] = ! empty($totalArr['attribution_next_day_open_rate']) ? $totalArr['attribution_next_day_open_rate'] : 0;
                    $totalArr['attribution_retention_2d_cnt'] = ! empty($totalArr['attribution_retention_2d_cnt']) ? $totalArr['attribution_retention_2d_cnt'] : 0;
                    $totalArr['attribution_retention_2d_cost'] = ! empty($totalArr['attribution_retention_2d_cost']) ? $totalArr['attribution_retention_2d_cost'] : 0;
                    $totalArr['attribution_retention_2d_rate'] = ! empty($totalArr['attribution_retention_2d_rate']) ? $totalArr['attribution_retention_2d_rate'] : 0;
                    $totalArr['attribution_retention_3d_cnt'] = ! empty($totalArr['attribution_retention_3d_cnt']) ? $totalArr['attribution_retention_3d_cnt'] : 0;
                    $totalArr['attribution_retention_3d_cost'] = ! empty($totalArr['attribution_retention_3d_cost']) ? $totalArr['attribution_retention_3d_cost'] : 0;
                    $totalArr['attribution_retention_3d_rate'] = ! empty($totalArr['attribution_retention_3d_rate']) ? $totalArr['attribution_retention_3d_rate'] : 0;
                    $totalArr['attribution_retention_4d_cnt'] = ! empty($totalArr['attribution_retention_4d_cnt']) ? $totalArr['attribution_retention_4d_cnt'] : 0;
                    $totalArr['attribution_retention_4d_cost'] = ! empty($totalArr['attribution_retention_4d_cost']) ? $totalArr['attribution_retention_4d_cost'] : 0;
                    $totalArr['attribution_retention_4d_rate'] = ! empty($totalArr['attribution_retention_4d_rate']) ? $totalArr['attribution_retention_4d_rate'] : 0;
                    $totalArr['attribution_retention_5d_cnt'] = ! empty($totalArr['attribution_retention_5d_cnt']) ? $totalArr['attribution_retention_5d_cnt'] : 0;
                    $totalArr['attribution_retention_5d_cost'] = ! empty($totalArr['attribution_retention_5d_cost']) ? $totalArr['attribution_retention_5d_cost'] : 0;
                    $totalArr['attribution_retention_5d_rate'] = ! empty($totalArr['attribution_retention_5d_rate']) ? $totalArr['attribution_retention_5d_rate'] : 0;
                    $totalArr['attribution_retention_6d_cnt'] = ! empty($totalArr['attribution_retention_6d_cnt']) ? $totalArr['attribution_retention_6d_cnt'] : 0;
                    $totalArr['attribution_retention_6d_cost'] = ! empty($totalArr['attribution_retention_6d_cost']) ? $totalArr['attribution_retention_6d_cost'] : 0;
                    $totalArr['attribution_retention_6d_rate'] = ! empty($totalArr['attribution_retention_6d_rate']) ? $totalArr['attribution_retention_6d_rate'] : 0;
                    $totalArr['attribution_retention_7d_cnt'] = ! empty($totalArr['attribution_retention_7d_cnt']) ? $totalArr['attribution_retention_7d_cnt'] : 0;
                    $totalArr['attribution_retention_7d_cost'] = ! empty($totalArr['attribution_retention_7d_cost']) ? $totalArr['attribution_retention_7d_cost'] : 0;
                    $totalArr['attribution_retention_7d_rate'] = ! empty($totalArr['attribution_retention_7d_rate']) ? $totalArr['attribution_retention_7d_rate'] : 0;
                    $totalArr['game_addiction'] = ! empty($totalArr['game_addiction']) ? $totalArr['game_addiction'] : 0;
                    $totalArr['game_addiction_cost'] = ! empty($totalArr['game_addiction_cost']) ? $totalArr['game_addiction_cost'] : 0;
                    $totalArr['game_addiction_rate'] = ! empty($totalArr['game_addiction_rate']) ? $totalArr['game_addiction_rate'] : 0;
                    $totalArr['pay_count'] = ! empty($totalArr['pay_count']) ? $totalArr['pay_count'] : 0;
                    $totalArr['active_pay_cost'] = ! empty($totalArr['active_pay_cost']) ? $totalArr['active_pay_cost'] : 0;
                    $totalArr['active_pay_rate'] = ! empty($totalArr['active_pay_rate']) ? $totalArr['active_pay_rate'] : 0;
                    $totalArr['active_pay_cost_yesterday'] = ! empty($totalArr['active_pay_cost_yesterday']) ? $totalArr['active_pay_cost_yesterday'] : 0;
                    $totalArr['game_pay_cost'] = ! empty($totalArr['game_pay_cost']) ? $totalArr['game_pay_cost'] : 0;
                    $totalArr['game_pay_count'] = ! empty($totalArr['game_pay_count']) ? $totalArr['game_pay_count'] : 0;
                    $totalArr['attribution_game_pay_7d_count'] = ! empty($totalArr['attribution_game_pay_7d_count']) ? $totalArr['attribution_game_pay_7d_count'] : 0;
                    $totalArr['attribution_game_pay_7d_cost'] = ! empty($totalArr['attribution_game_pay_7d_cost']) ? $totalArr['attribution_game_pay_7d_cost'] : 0;
                    $totalArr['attribution_active_pay_7d_per_count'] = ! empty($totalArr['attribution_active_pay_7d_per_count']) ? $totalArr['attribution_active_pay_7d_per_count'] : 0;
                    $totalArr['loan_completion'] = ! empty($totalArr['loan_completion']) ? $totalArr['loan_completion'] : 0;
                    $totalArr['loan_completion_cost'] = ! empty($totalArr['loan_completion_cost']) ? $totalArr['loan_completion_cost'] : 0;
                    $totalArr['loan_completion_rate'] = ! empty($totalArr['loan_completion_rate']) ? $totalArr['loan_completion_rate'] : 0;
                    $totalArr['pre_loan_credit'] = ! empty($totalArr['pre_loan_credit']) ? $totalArr['pre_loan_credit'] : 0;
                    $totalArr['pre_loan_credit_cost'] = ! empty($totalArr['pre_loan_credit_cost']) ? $totalArr['pre_loan_credit_cost'] : 0;
                    $totalArr['loan_credit'] = ! empty($totalArr['loan_credit']) ? $totalArr['loan_credit'] : 0;
                    $totalArr['loan_credit_cost'] = ! empty($totalArr['loan_credit_cost']) ? $totalArr['loan_credit_cost'] : 0;
                    $totalArr['loan_credit_rate'] = ! empty($totalArr['loan_credit_rate']) ? $totalArr['loan_credit_rate'] : 0;
                    $totalArr['in_app_uv'] = ! empty($totalArr['in_app_uv']) ? $totalArr['in_app_uv'] : 0;
                    $totalArr['in_app_detail_uv'] = ! empty($totalArr['in_app_detail_uv']) ? $totalArr['in_app_detail_uv'] : 0;
                    $totalArr['in_app_cart'] = ! empty($totalArr['in_app_cart']) ? $totalArr['in_app_cart'] : 0;
                    $totalArr['in_app_pay'] = ! empty($totalArr['in_app_pay']) ? $totalArr['in_app_pay'] : 0;
                    $totalArr['in_app_order'] = ! empty($totalArr['in_app_order']) ? $totalArr['in_app_order'] : 0;
                    $totalArr['phone'] = ! empty($totalArr['phone']) ? $totalArr['phone'] : 0;
                    $totalArr['form'] = ! empty($totalArr['form']) ? $totalArr['form'] : 0;
                    $totalArr['total_play'] = ! empty($totalArr['total_play']) ? $totalArr['total_play'] : 0;
                    $totalArr['valid_play'] = ! empty($totalArr['valid_play']) ? $totalArr['valid_play'] : 0;
                    $totalArr['valid_play_cost'] = ! empty($totalArr['valid_play_cost']) ? $totalArr['valid_play_cost'] : 0;
                    $totalArr['valid_play_rate'] = ! empty($totalArr['valid_play_rate']) ? $totalArr['valid_play_rate'] : 0;
                    $totalArr['share'] = ! empty($totalArr['share']) ? $totalArr['share'] : 0;
                    $totalArr['comment'] = ! empty($totalArr['comment']) ? $totalArr['comment'] : 0;
                    $totalArr['like'] = ! empty($totalArr['like']) ? $totalArr['like'] : 0;
                    $totalArr['follow'] = ! empty($totalArr['follow']) ? $totalArr['follow'] : 0;
                    $totalArr['message_actio'] = ! empty($totalArr['message_actio']) ? $totalArr['message_actio'] : 0;

                    $totalArr['attribution_game_in_app_ltv_1day'] = ! empty($totalArr['attribution_game_in_app_ltv_1day']) ? $totalArr['attribution_game_in_app_ltv_1day'] : 0; // 当日付费金额
                    $totalArr['attribution_game_in_app_roi_1day'] = ! empty($totalArr['attribution_game_in_app_roi_1day']) ? $totalArr['attribution_game_in_app_roi_1day'] : 0; // 当日付费ROI
                    // 总花费
                    if (in_array('cost', $add_fields)) {
                        $totalArr['cost'] = array_sum(array_column($dataArrs, 'cost'));
                    }
                    // 展示数
                    if (in_array('show', $add_fields)) {
                        $totalArr['show'] = array_sum(array_column($dataArrs, 'show'));
                    }
                    // 平均千次展现费用
                    if (in_array('avg_show_cost', $add_fields)) {
                        $totalArr['avg_show_cost'] = ! empty($totalArr['show']) ? round($totalArr['cost'] / $totalArr['show'] * 1000, 2) : 0;
                    }
                    // 点击数
                    if (in_array('click', $add_fields)) {
                        $totalArr['click'] = array_sum(array_column($dataArrs, 'click'));
                    }
                    // 平均点击单价
                    if (in_array('avg_click_cost', $add_fields)) {
                        $totalArr['avg_click_cost'] = ! empty($totalArr['click']) ? round($totalArr['cost'] / $totalArr['click'], 2) : 0;
                    }
                    // 点击率
                    if (in_array('ctr', $add_fields)) {
                        $totalArr['ctr'] = ! empty($totalArr['show']) ? round($totalArr['click'] / $totalArr['show'] * 100, 2) . '%' : 0;
                    }
                    // 折后消耗
                    if (in_array('cash_cost', $add_fields)) {
                        $totalArr['cash_cost'] = array_sum(array_column($dataArrs, 'cash_cost'));
                    }
                    // 折后成本
                    if (in_array('folding_convert', $add_fields)) {
                        $totalArr['folding_convert'] = array_sum(array_column($dataArrs, 'folding_convert'));
                    }
                    // 昨日同时段消耗
                    if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                        $totalArr['yesterday_cost'] = array_sum(array_column($dataArrs, 'yesterday_cost'));
                    }
                    // 昨日同时段消耗环比
                    if (in_array('yesterday_cost', $add_fields) || in_array('yesterday_cost_rate', $add_fields)) {
                        $x = bcsub(strval($totalArr['cost']), strval($totalArr['yesterday_cost']), 3);
                        $totalArr['yesterday_cost_rate'] = ! empty($totalArr['yesterday_cost']) ? round($x / $totalArr['yesterday_cost'] * 100, 2) . '%' : 0;
                    }
                    // 转化数
                    if (in_array('convert', $add_fields)) {
                        $totalArr['convert'] = array_sum(array_column($dataArrs, 'convert'));
                    }
                    // 转化成本
                    if (in_array('convert_cost', $add_fields)) {
                        $totalArr['convert_cost'] = ! empty($totalArr['convert']) ? round($totalArr['cost'] / $totalArr['convert'], 2) : 0;
                    }
                    // 转化率
                    if (in_array('convert_rate', $add_fields)) {
                        $totalArr['convert_rate'] = ! empty($totalArr['click']) ? round($totalArr['convert'] / $totalArr['click'] * 100, 2) . '%' : 0;
                    }
                    // 深度转化数
                    if (in_array('deep_convert', $add_fields)) {
                        $totalArr['deep_convert'] = array_sum(array_column($dataArrs, 'deep_convert'));
                    }
                    // 深度转化成本
                    if (in_array('deep_convert_cost', $add_fields)) {
                        $totalArr['deep_convert_cost'] = ! empty($totalArr['deep_convert']) ? round($totalArr['cost'] / $totalArr['deep_convert'], 2) : 0;
                    }
                    // 深度转化率
                    if (in_array('deep_convert_rate', $add_fields)) {
                        $totalArr['deep_convert_rate'] = ! empty($totalArr['convert']) ? round($totalArr['deep_convert'] / $totalArr['convert'] * 100, 2) . '%' : 0;
                    }
                    // 转化数（计费时间）
                    if (in_array('attribution_convert', $add_fields)) {
                        $totalArr['attribution_convert'] = array_sum(array_column($dataArrs, 'attribution_convert'));
                    }
                    // 转化成本（计费时间）
                    if (in_array('attribution_convert_cost', $add_fields)) {
                        $totalArr['attribution_convert_cost'] = ! empty($totalArr['attribution_convert']) ? round($totalArr['cost'] / $totalArr['attribution_convert'], 2) : 0;
                    }
                    // 深度转化数（计费时间）
                    if (in_array('attribution_deep_convert', $add_fields)) {
                        $totalArr['attribution_deep_convert'] = array_sum(array_column($dataArrs, 'attribution_deep_convert'));
                    }
                    // 深度转化成本（计费时间）
                    if (in_array('attribution_deep_convert_cost', $add_fields)) {
                        $totalArr['attribution_deep_convert_cost'] = ! empty($totalArr['attribution_deep_convert']) ? round($totalArr['cost'] / $totalArr['attribution_deep_convert'], 2) : 0;
                    }
                    // 安卓下载开始数
                    if (in_array('download_start', $add_fields)) {
                        $totalArr['download_start'] = array_sum(array_column($dataArrs, 'download_start'));
                    }
                    // 安卓下载开始成本
                    if (in_array('download_start_cost', $add_fields)) {
                        $totalArr['download_start_cost'] = ! empty($totalArr['download_start']) ? round($totalArr['cost'] / $totalArr['download_start'], 2) : 0;
                    }
                    // 安卓下载开始率
                    if (in_array('download_start_rate', $add_fields)) {
                        $totalArr['download_start_rate'] = ! empty($totalArr['click']) ? round($totalArr['download_start'] / $totalArr['click'] * 100, 2) . '%' : 0;
                    }
                    // 安卓下载完成数
                    if (in_array('download_finish', $add_fields)) {
                        $totalArr['download_finish'] = array_sum(array_column($dataArrs, 'download_finish'));
                    }
                    // 安卓下载完成成本
                    if (in_array('download_finish_cost', $add_fields)) {
                        $totalArr['download_finish_cost'] = ! empty($totalArr['download_finish']) ? round($totalArr['cost'] / $totalArr['download_finish'], 2) : 0;
                    }
                    // 安卓下载完成率
                    if (in_array('download_finish_rate', $add_fields)) {
                        $totalArr['download_finish_rate'] = ! empty($totalArr['download_start']) ? round($totalArr['download_finish'] / $totalArr['download_start'] * 100, 2) . '%' : 0;
                    }
                    // 安卓安装完成数
                    if (in_array('install_finish', $add_fields)) {
                        $totalArr['install_finish'] = array_sum(array_column($dataArrs, 'install_finish'));
                    }
                    // 安卓安装完成成本
                    if (in_array('install_finish_cost', $add_fields)) {
                        $totalArr['install_finish_cost'] = ! empty($totalArr['install_finish']) ? round($totalArr['cost'] / $totalArr['install_finish'], 2) : 0;
                    }
                    // 安卓安装完成率
                    if (in_array('install_finish_rate', $add_fields)) {
                        $totalArr['install_finish_rate'] = ! empty($totalArr['download_finish']) ? round($totalArr['install_finish'] / $totalArr['download_finish'] * 100, 2) . '%' : 0;
                    }
                    // 激活数
                    if (in_array('active', $add_fields)) {
                        $totalArr['active'] = array_sum(array_column($dataArrs, 'active'));
                    }
                    // 激活成本
                    if (in_array('active_cost', $add_fields)) {
                        $totalArr['active_cost'] = ! empty($totalArr['active']) ? round($totalArr['cost'] / $totalArr['active'], 2) : 0;
                    }
                    // 激活率
                    if (in_array('active_rate', $add_fields)) {
                        $totalArr['active_rate'] = ! empty($totalArr['click']) ? round($totalArr['active'] / $totalArr['click'] * 100, 2) . '%' : 0;
                    }
                    // 注册数
                    if (in_array('register', $add_fields)) {
                        $totalArr['register'] = array_sum(array_column($dataArrs, 'register'));
                    }
                    // 注册成本
                    if (in_array('active_register_cost', $add_fields)) {
                        $totalArr['active_register_cost'] = ! empty($totalArr['register']) ? round($totalArr['cost'] / $totalArr['register'], 2) : 0;
                    }
                    // 注册率
                    if (in_array('active_register_rate', $add_fields)) {
                        $totalArr['active_register_rate'] = ! empty($totalArr['active']) ? round($totalArr['register'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 关键行为数
                    if (in_array('game_addiction', $add_fields)) {
                        $totalArr['game_addiction'] = array_sum(array_column($dataArrs, 'game_addiction'));
                    }
                    // 关键行为成本
                    if (in_array('game_addiction_cost', $add_fields)) {
                        $totalArr['game_addiction_cost'] = ! empty($totalArr['game_addiction']) ? round($totalArr['cost'] / $totalArr['game_addiction'], 2) : 0;
                    }
                    // 关键行为率
                    if (in_array('game_addiction_rate', $add_fields)) {
                        $totalArr['game_addiction_rate'] = ! empty($totalArr['active']) ? round($totalArr['game_addiction'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 首次付费数
                    if (in_array('pay_count', $add_fields)) {
                        $totalArr['pay_count'] = array_sum(array_column($dataArrs, 'pay_count'));
                    }
                    // 首次付费成本
                    if (in_array('active_pay_cost', $add_fields)) {
                        $totalArr['active_pay_cost'] = ! empty($totalArr['pay_count']) ? round($totalArr['cost'] / $totalArr['pay_count'], 2) : 0;
                    }
                    // 首次付费率
                    if (in_array('active_pay_rate', $add_fields)) {
                        $totalArr['active_pay_rate'] = ! empty($totalArr['active']) ? round($totalArr['pay_count'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 付费成本环比
                    if (in_array('active_pay_ringgit', $add_fields)) {
                        $totalArr['active_pay_ringgit'] = ! empty($totalArr['active_pay_cost_yesterday']) ? round($totalArr['active_pay_cost'] / $totalArr['active_pay_cost_yesterday'] - 1, 2) : 0;
                    }
                    // 付费次数
                    if (in_array('game_pay_count', $add_fields)) {
                        $totalArr['game_pay_count'] = array_sum(array_column($dataArrs, 'game_pay_count'));
                    }
                    // 付费成本
                    if (in_array('game_pay_cost', $add_fields)) {
                        $totalArr['game_pay_cost'] = ! empty($totalArr['game_pay_count']) ? round($totalArr['cost'] / $totalArr['game_pay_count'], 2) : 0;
                    }
                    // 付费率
                    if (in_array('game_pay_rate', $add_fields)) {
                        $totalArr['game_pay_rate'] = ! empty($totalArr['active']) ? round($totalArr['game_pay_count'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 7日付费次数(激活时间)
                    if (in_array('attribution_game_pay_7d_count', $add_fields)) {
                        $totalArr['attribution_game_pay_7d_count'] = array_sum(array_column($dataArrs, 'attribution_game_pay_7d_count'));
                    }
                    // 7日付费成本(激活时间)
                    if (in_array('attribution_game_pay_7d_cost', $add_fields)) {
                        $totalArr['attribution_game_pay_7d_cost'] = ! empty($totalArr['attribution_game_pay_7d_count']) ? round($totalArr['cost'] / $totalArr['attribution_game_pay_7d_count'], 2) : 0;
                    }
                    // 7日人均付费次数(激活时间)
                    if (in_array('attribution_active_pay_7d_per_count', $add_fields)) {
                        $totalArr['attribution_active_pay_7d_per_count'] = array_sum(array_column($dataArrs, 'attribution_active_pay_7d_per_count'));
                    }
                    // 次留数
                    if (in_array('attribution_next_day_open_cnt', $add_fields) || in_array('attribution_next_day_open_cnt', $add_fields) || in_array('attribution_next_day_open_cost', $add_fields) || in_array('attribution_next_day_open_rate', $add_fields)) {
                        $totalArr['attribution_next_day_open_cnt'] = array_sum($ciDayEventNextDayStayArr);
                    }
                    // 次留成本
                    if (in_array('attribution_next_day_open_cnt', $add_fields) || in_array('attribution_next_day_open_cost', $add_fields) || in_array('attribution_next_day_open_rate', $add_fields)) {
                        $ciDayChargeSum = array_sum($ciDayChargeArr);
                        $totalArr['attribution_next_day_open_cost'] = ! empty($totalArr['attribution_next_day_open_cnt']) ? round($ciDayChargeSum / $totalArr['attribution_next_day_open_cnt'], 2) : 0;
                    }
                    // 次留率
                    if (in_array('attribution_next_day_open_cnt', $add_fields) || in_array('attribution_next_day_open_cost', $add_fields) || in_array('attribution_next_day_open_rate', $add_fields)) {
                        $ciDayActivationSum = array_sum($ciDayActivationArr);
                        $totalArr['attribution_next_day_open_rate'] = ! empty($ciDayChargeSum) ? round($totalArr['attribution_next_day_open_cnt'] / $ciDayActivationSum * 100, 2) . '%' : 0;
                    }
                    // 2日留存数
                    if (in_array('attribution_retention_2d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_2d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_2d_cnt'));
                    }
                    // 2日留存成本
                    if (in_array('attribution_retention_2d_cost', $add_fields)) {
                        $totalArr['attribution_retention_2d_cost'] = ! empty($totalArr['attribution_retention_2d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_2d_cnt'], 2) : 0;
                    }
                    // 2日留存率
                    if (in_array('attribution_retention_2d_rate', $add_fields)) {
                        $totalArr['attribution_retention_2d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_2d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 3日留存数
                    if (in_array('attribution_retention_3d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_3d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_3d_cnt'));
                    }
                    // 3日留存成本
                    if (in_array('attribution_retention_3d_cost', $add_fields)) {
                        $totalArr['attribution_retention_3d_cost'] = ! empty($totalArr['attribution_retention_3d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_3d_cnt'], 2) : 0;
                    }
                    // 3日留存率
                    if (in_array('attribution_retention_3d_rate', $add_fields)) {
                        $totalArr['attribution_retention_3d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_3d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 4日留存数
                    if (in_array('attribution_retention_4d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_4d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_4d_cnt'));
                    }
                    // 4日留存成本
                    if (in_array('attribution_retention_4d_cost', $add_fields)) {
                        $totalArr['attribution_retention_4d_cost'] = ! empty($totalArr['attribution_retention_4d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_4d_cnt'], 2) : 0;
                    }
                    // 4日留存率
                    if (in_array('attribution_retention_4d_rate', $add_fields)) {
                        $totalArr['attribution_retention_4d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_4d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 5日留存数
                    if (in_array('attribution_retention_5d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_5d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_5d_cnt'));
                    }
                    // 5日留存成本
                    if (in_array('attribution_retention_5d_cost', $add_fields)) {
                        $totalArr['attribution_retention_5d_cost'] = ! empty($totalArr['attribution_retention_5d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_5d_cnt'], 2) : 0;
                    }
                    // 5日留存率
                    if (in_array('attribution_retention_5d_rate', $add_fields)) {
                        $totalArr['attribution_retention_5d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_5d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 6日留存数
                    if (in_array('attribution_retention_6d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_6d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_6d_cnt'));
                    }
                    // 6日留存成本
                    if (in_array('attribution_retention_6d_cost', $add_fields)) {
                        $totalArr['attribution_retention_6d_cost'] = ! empty($totalArr['attribution_retention_6d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_6d_cnt'], 2) : 0;
                    }
                    // 6日留存率
                    if (in_array('attribution_retention_6d_rate', $add_fields)) {
                        $totalArr['attribution_retention_6d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_6d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 7日留存数
                    if (in_array('attribution_retention_7d_cnt', $add_fields)) {
                        $totalArr['attribution_retention_7d_cnt'] = array_sum(array_column($dataArrs, 'attribution_retention_7d_cnt'));
                    }
                    // 7日留存成本
                    if (in_array('attribution_retention_7d_cost', $add_fields)) {
                        $totalArr['attribution_retention_7d_cost'] = ! empty($totalArr['attribution_retention_7d_cnt']) ? round($totalArr['cost'] / $totalArr['attribution_retention_7d_cnt'], 2) : 0;
                    }
                    // 7日留存率
                    if (in_array('attribution_retention_7d_rate', $add_fields)) {
                        $totalArr['attribution_retention_7d_rate'] = ! empty($totalArr['active']) ? round($totalArr['attribution_retention_7d_cnt'] / $totalArr['active'] * 100, 2) . '%' : 0;
                    }
                    // 完件数
                    if (in_array('loan_completion', $add_fields)) {
                        $totalArr['loan_completion'] = array_sum(array_column($dataArrs, 'loan_completion'));
                    }
                    // 完件成本
                    if (in_array('loan_completion_cost', $add_fields)) {
                        $totalArr['loan_completion_cost'] = ! empty($totalArr['loan_completion']) ? round($totalArr['cost'] / $totalArr['loan_completion'], 2) : 0;
                    }
                    // 完件率
                    if (in_array('loan_completion_rate', $add_fields)) {
                        $totalArr['loan_completion_rate'] = ! empty($totalArr['register']) ? round($totalArr['loan_completion'] / $totalArr['register'] * 100, 2) . '%' : 0;
                    }
                    // 预授信数
                    if (in_array('pre_loan_credit', $add_fields)) {
                        $totalArr['pre_loan_credit'] = array_sum(array_column($dataArrs, 'pre_loan_credit'));
                    }
                    // 预授信成本
                    if (in_array('pre_loan_credit_cost', $add_fields)) {
                        $totalArr['pre_loan_credit_cost'] = ! empty($totalArr['pre_loan_credit']) ? round($totalArr['cost'] / $totalArr['pre_loan_credit'], 2) : 0;
                    }
                    // 授信数
                    if (in_array('loan_credit', $add_fields)) {
                        $totalArr['loan_credit'] = array_sum(array_column($dataArrs, 'loan_credit'));
                    }
                    // 授信成本
                    if (in_array('loan_credit_cost', $add_fields)) {
                        $totalArr['loan_credit_cost'] = ! empty($totalArr['loan_credit']) ? round($totalArr['cost'] / $totalArr['loan_credit'], 2) : 0;
                    }
                    // 授信率
                    if (in_array('loan_credit_rate', $add_fields)) {
                        $totalArr['loan_credit_rate'] = ! empty($totalArr['loan_completion']) ? round($totalArr['loan_credit'] / $totalArr['loan_completion'] * 100, 2) . '%' : 0;
                    }
                    // APP内访问
                    if (in_array('in_app_uv', $add_fields)) {
                        $totalArr['in_app_uv'] = array_sum(array_column($dataArrs, 'in_app_uv'));
                    }
                    // APP内访问详情页
                    if (in_array('in_app_detail_uv', $add_fields)) {
                        $totalArr['in_app_detail_uv'] = array_sum(array_column($dataArrs, 'in_app_detail_uv'));
                    }
                    // APP内加入购物车
                    if (in_array('in_app_cart', $add_fields)) {
                        $totalArr['in_app_cart'] = array_sum(array_column($dataArrs, 'in_app_cart'));
                    }
                    // APP内付费
                    if (in_array('in_app_pay', $add_fields)) {
                        $totalArr['in_app_pay'] = array_sum(array_column($dataArrs, 'in_app_pay'));
                    }
                    // APP内下单
                    if (in_array('in_app_order', $add_fields)) {
                        $totalArr['in_app_order'] = array_sum(array_column($dataArrs, 'in_app_order'));
                    }
                    // 点击电话按钮
                    if (in_array('phone', $add_fields)) {
                        $totalArr['phone'] = array_sum(array_column($dataArrs, 'phone'));
                    }
                    // 表单提交
                    if (in_array('form', $add_fields)) {
                        $totalArr['form'] = array_sum(array_column($dataArrs, 'form'));
                    }
                    // 播放数
                    if (in_array('total_play', $add_fields)) {
                        $totalArr['total_play'] = array_sum(array_column($dataArrs, 'total_play'));
                    }
                    // 有效播放数
                    if (in_array('valid_play', $add_fields)) {
                        $totalArr['valid_play'] = array_sum(array_column($dataArrs, 'valid_play'));
                    }
                    // 有效播放成本
                    if (in_array('valid_play_cost', $add_fields)) {
                        $totalArr['valid_play_cost'] = ! empty($totalArr['valid_play']) ? round($totalArr['cost'] / $totalArr['valid_play'], 2) : 0;
                    }
                    // 有效播放率
                    if (in_array('valid_play_rate', $add_fields)) {
                        $totalArr['valid_play_rate'] = ! empty($totalArr['show']) ? round($totalArr['valid_play'] / $totalArr['show'] * 100, 2) . '%' : 0;
                    }
                    // 分享数
                    if (in_array('share', $add_fields)) {
                        $totalArr['share'] = array_sum(array_column($dataArrs, 'share'));
                    }
                    // 评论提交数
                    if (in_array('comment', $add_fields)) {
                        $totalArr['comment'] = array_sum(array_column($dataArrs, 'comment'));
                    }
                    // 点赞数
                    if (in_array('like', $add_fields)) {
                        $totalArr['like'] = array_sum(array_column($dataArrs, 'like'));
                    }
                    // 新增关注数
                    if (in_array('follow', $add_fields)) {
                        $totalArr['follow'] = array_sum(array_column($dataArrs, 'follow'));
                    }
                    // 私信数
                    if (in_array('message_action', $add_fields)) {
                        $totalArr['message_action'] = array_sum(array_column($dataArrs, 'message_action'));
                    }
                    // 当日付费金额
                    if (in_array('attribution_game_in_app_ltv_1day', $add_fields)) {
                        $totalArr['attribution_game_in_app_ltv_1day'] = array_sum(array_column($dataArrs, 'attribution_game_in_app_ltv_1day'));
                    }
                    // 当日付费ROI
                    if (in_array('attribution_game_in_app_roi_1day', $add_fields)) {
                        $totalArr['attribution_game_in_app_roi_1day'] = ! empty($totalArr['cost']) ? round($totalArr['attribution_game_in_app_ltv_1day'] / $totalArr['cost'] * 100, 2) . '%' : 0;
                    }
                    // 自定义数据
                    if (! empty($custom_fields_name)) {
                        foreach ($custom_fields_name as $cfnv) {
                            $molecule = ! empty($totalArr[$cfnv['molecule']]) ? str_replace('%', '', $totalArr[$cfnv['molecule']]) : 0;
                            $denominator = ! empty($totalArr[$cfnv['denominator']]) ? str_replace('%', '', $totalArr[$cfnv['denominator']]) : 0;
                            $totalArr[$cfnv['field']] = $denominator > 0 ? round($molecule / $denominator, 2) : 0;
                        }
                    }
                }
            }
            $result['data'] = $dataArr;
            $result['total'] = $totalArr;
            // 返回信息
            return $result;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取广告主
    public static function importAccountReport(int $statDate = 0, int $sendHour = 0, string $advertiserId = '', int $dateType = 1)
    {
        try {
            $data = (new ReportRecordPushOeAccountModel())
                ->where('advertiser_id', $advertiserId)
                ->where('stat_date', $statDate)
                ->where('stat_hour', '<=', $sendHour)
                ->where('date_type', $dateType)
                ->order('stat_hour ASC')
                ->select();
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v['data_content'], true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取计划
    public static function importCampaignReport(int $statDate = 0, int $sendTime = 0, string $advertiserId = '', int $dateType = 1)
    {
        $where = [
            'stat_date' => $statDate,
            'stat_hour' => $sendTime,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
        ];
        try {
            $data = (new ReportRecordPushOeCampaignModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    // 获取广告组
    public static function importUnitReport(int $statDate = 0, int $sendTime = 0, string $advertiserId = '', int $dateType = 1)
    {
        $where = [
            'stat_date' => $statDate,
            'stat_hour' => $sendTime,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
        ];
        try {
            $data = (new ReportRecordPushOeUnitModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getAccountCost(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushOeAccountModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', '<=', $statHour)
            ->where('date_type', $dateType)
            ->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getCampaignCost(string $advertiserId = '', string $id = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushOeCampaignModel())
            ->where('advertiser_id', $advertiserId)
            ->where('campaign_id', $id)
            ->where('stat_hour', '<=', $statHour)
            ->where('stat_date', $statDate)
            ->where('date_type', $dateType)
            ->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取消耗的总和.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getUnitCost(string $advertiserId = '', string $id = '', int $statDate = 0, int $statHour = 0, int $dateType = 0, string $field = '')
    {
        $data = (new ReportRecordPushOeUnitModel())->where('advertiser_id', $advertiserId)
            ->where('unit_id', $id)
            ->where('stat_hour', '<=', $statHour)
            ->where('stat_date', $statDate)
            ->where('date_type', ReportRecordEnum::REPORT_DATE_TYPE_TIME)->select();
        $sum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $sum = $val['data_content'][$field] + $sum;
            }
        }
        return $sum;
    }

    /**
     * 获取次日次留.
     */
    public static function getAccountSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushOeAccountModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            // ->where('stat_hour', $statHour)
            ->where('date_type', $report_date_type)
            ->select();
        $attributionNextDayStaySum = 0;
        $costSum = 0;
        $activeSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $costSum += $val['data_content']['cost']; // 消耗
                $activeSum += $val['data_content']['active']; // 激活数
                $attributionNextDayStaySum += $val['data_content']['attribution_next_day_open_cnt']; // 次留数
            }
        }
        return [
            'attribution_next_day_open_cnt' => $attributionNextDayStaySum,
            'cost' => $costSum,
            'active' => $activeSum,
        ];
    }

    /**
     * 获取次日次留.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getUnitSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushOeUnitModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', $statHour)
            ->where('date_type', $report_date_type)
            ->select();
        $attributionNextDayStaySum = 0;
        $costSum = 0;
        $activeSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $attributionNextDayStaySum += $val['data_content']['attribution_next_day_open_cnt']; // 次留数
                $costSum += $val['data_content']['cost']; // 消耗
                $activeSum += $val['data_content']['active']; // 激活数
            }
        }
        return [
            'attribution_next_day_open_cnt' => $attributionNextDayStaySum,
            'cost' => $costSum,
            'active' => $activeSum,
        ];
    }

    /**
     * 获取次日次留.
     * @return int|mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getCampaignSecondaryStation(string $advertiserId = '', int $statDate = 0, int $statHour = 0, int $report_date_type = 0): array
    {
        $data = (new ReportRecordPushOeCampaignModel())
            ->where('advertiser_id', $advertiserId)
            ->where('stat_date', $statDate)
            ->where('stat_hour', $statHour)
            ->where('date_type', $report_date_type)
            ->select();
        $attributionNextDayStaySum = 0;
        $costSum = 0;
        $activeSum = 0;
        if ($data) {
            $data = $data->toArray();
            foreach ($data as $val) {
                $val['data_content'] = json_decode($val['data_content'], true);
                $attributionNextDayStaySum += $val['data_content']['attribution_next_day_open_cnt']; // 次留数
                $costSum += $val['data_content']['cost']; // 消耗
                $activeSum += $val['data_content']['active']; // 激活数
            }
        }
        return [
            'attribution_next_day_open_cnt' => $attributionNextDayStaySum,
            'cost' => $costSum,
            'active' => $activeSum,
        ];
    }

    // 获取广告主
    public static function importAccountReportDate(int $statDate = 0, int $sendHour = 0, string $advertiserId = '', int $dateType = 1)
    {
        $where = [
            'stat_date' => $statDate,
            'stat_hour' => $sendHour,
            'advertiser_id' => $advertiserId,
            'date_type' => $dateType,
        ];
        try {
            $data = (new ReportRecordPushOeAccountDateModel())->where($where)->column('data_content');
            $datas = [];
            foreach ($data as $v) {
                $datas[] = json_decode($v, true);
            }
            return $datas;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }
}
